﻿using SuperKit.WindowForm.GalleryWindow;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace SuperKit {
    public class SuperPlayerViewModel : FileViewModelBase {
        public const double MB = 1024 * 1024;
        public const double GB = 1024 * 1024 * 1024;
        public string Name { get; set; }
        public FileInfo Info { get; set; }
        public ClipCollections Clips { get; }
        private bool _checked;
        public bool Checked {
            get => _checked;
            set {
                _checked = value;
                OnPropertyChanged("Checked");
            } 
        }

        public SuperPlayerViewModel(FileInfo info) {
            Name = info.Name;
            Info = info;
            Clips = new ClipCollections();
            _checked = false;
        }

        public override string ToString() {
            return Name;
        }

        public string Title {
            get {
                var mb = Info.Length / MB;
                var size_str = string.Empty;
                if (mb > 1024) {
                    var gb = mb / 1024;
                    size_str = $"{gb:N2} GB";
                } else {
                    size_str = $"{mb:N0} MB";
                }
                return $"{Info.Name}[{size_str}]";
            }
        }

        public void AddCut(int current, int length) {
            Clips.AddCut(current, length);
        }

        public void Rename(string newName) {
            // 建立新名称
            var dir = Path.GetDirectoryName(Info.FullName);
            var newPath = Path.Combine(dir, newName);
            // 如果已经存在，则不改名
            if (File.Exists(newPath)) {
                return;
            }
            // 改名
            File.Move(Info.FullName, newPath);

            // 重新配置属性
            Info = new FileInfo(newPath);
            Name = Info.Name;
            OnPropertyChanged("Title");
        }
    }

}
